/**
 * 
 */
package com.xy.frame.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xy.commons.PageUtil;
import com.xy.frame.dao.RoleMapper;

/**
 * @author admin
 * 
 */
@Service
public class RoleService {
	@Autowired
	private RoleMapper roleMapper;

	/***
	 * 加载所有的角色
	 */
	public List<Map<String, Object>> loadRoleTree() {
		List<Map<String, Object>> list = roleMapper.loadRoleTree();
		// 转换成树形结构
		List<Map<String, Object>> treeList = Lists.newArrayList();
		for (Map<String, Object> map : list) {
			// 0 1 2 3 4
			if (map.get("F_Role_LEVEL").toString().equals("0")) {
				Map<String, Object> node1 = Maps.newHashMap();
				node1.putAll(map);
				node1.put("child", getChild(Lists.newArrayList(list), map));
				treeList.add(node1);
			}
		}
		return treeList ;
	}

	/**
	 * 获取当前节点的所有子节点
	 * 
	 * @param listcopy
	 * @param currentNode
	 * @return
	 */
	private List<Map<String, Object>> getChild(List<Map<String, Object>> listcopy, Map<String, Object> currentNode) {
		List<Map<String, Object>> subList = Lists.newArrayList();
		String RoleNum = currentNode.get("F_Role_NUM").toString();
		String RoleLevel = currentNode.get("F_Role_LEVEL").toString();
		for (Map<String, Object> map : listcopy) {
			Map<String, Object> subMap = Maps.newHashMap(map);
			String parentRoleNum = subMap.get("F_Role_PARENT") != null ? subMap.get("F_Role_PARENT").toString() : "";
			if (parentRoleNum.equals(RoleNum)) {
				String subRoleLevel = subMap.get("F_Role_LEVEL").toString();
				if ((Integer.valueOf(RoleLevel).intValue() + 1) == Integer.valueOf(subRoleLevel)) {
					subMap.put("child", getChild(Lists.newArrayList(listcopy), subMap));
				}
				subList.add(subMap);
			}
		}
		return subList;
	}

	/**
	 * @param params
	 * @return
	 * 
	 * 查询角色列表分页
	 */
	public Map<String, Object> queryRoleListByPage(Map<String, Object> params) {
        List<Map<String,Object>> items = roleMapper.queryRoleList(params);
        Integer count = roleMapper.queryRoleListCount(params);
        Map<String, Object> pages = Maps.newHashMap();
        pages.put(PageUtil.ITEMS, items);
        pages.put(PageUtil.TOTALS, count);
		return pages;
	}
	/**
	 * @param params
	 * @return
	 * 
	 * 查询角色列表分页
	 */
	public List<Map<String,Object>> queryRoleList(Map<String, Object> params) {
		return roleMapper.queryRoleList(params);
	}
    public Map<String, Object> getRole(String roleId) {
    	Map<String,Object> map = Maps.newHashMap();
    	map.put("roleNum", roleId);
    	List<Map<String,Object>> list = roleMapper.queryRoleList(map);
        return list.get(0);
    }
    @Transactional(rollbackFor=Exception.class)
	public void deleteRole(String roleNum) throws Exception {
    	roleMapper.deletePermission(roleNum);
    	roleMapper.deleteRole(roleNum);
	}
 
    @Transactional(rollbackFor=Exception.class)
	public void addRoleOrUpdate(Map<String, Object>  roleMap) throws Exception {
		if("1".equals(roleMap.get("type").toString())){
			roleMapper.addRole(roleMap);
		}else{
			roleMapper.updateRole(roleMap);
		}
	}
    public List<Map<String,Object>> queryAuthMenu(String loginNum){
    	Map<String,Object> loginNummap = Maps.newHashMap();
		loginNummap.put("loginNum", loginNum);
    	return  roleMapper.queryAuthMenu(loginNummap);

    }
	public String queryAuthMenuTree(String loginNum) {
		Map<String,Object> loginNummap = Maps.newHashMap();
		loginNummap.put("loginNum", loginNum);
		 List<Map<String,Object>>  userNodes = roleMapper.queryAuthMenu(loginNummap);
		 //转换成zTree JSON 数据格式
		 List<Map<String,Object>> zTreeNode = Lists.newArrayList();
		 for(Map<String,Object> map :userNodes){
			 zTreeNode.add(getNode(map));
		 }
		return JSONArray.toJSONString(zTreeNode);
	}

	private Map<String, Object> getNode(Map<String, Object> map) {
		Map<String,Object> node = Maps.newHashMap();
		
		node.put("id",map.get("F_MENU_NUM") );
		if(map.get("F_MENU_PARENT")==null){
			node.put("pId", "0");//根结点
		}else{
			node.put("pId", map.get("F_MENU_PARENT"));
		}
		node.put("name", map.get("F_MENU_TITLE"));
		if(map.get("F_MENU_LEVEL")!=null && map.get("F_MENU_LEVEL").toString().equals("0")){
			node.put("open","true" );
		}
		if(map.get("F_AUTH_NUM")!=null ){
			node.put("checked","true");
		}
		return node;
	}
	@Transactional(rollbackFor=Exception.class)
	public void savePermission(Map<String, Object> params)throws Exception {
		// 删除当前角色对应所有的权限
		roleMapper.deletePermission(params.get("roleNum").toString());
		// 重新插入当前角色权限
		roleMapper.insertPermission(params);

	}
}
